package com.atguigu.tms.mock.service.impl;

import com.atguigu.tms.mock.bean.LineBaseInfo;
import com.atguigu.tms.mock.bean.LineBaseShift;
import com.atguigu.tms.mock.bean.TruckDriver;
import com.atguigu.tms.mock.bean.TruckInfo;
import com.atguigu.tms.mock.config.AppConfig;
import com.atguigu.tms.mock.mapper.LineBaseShiftMapper;
import com.atguigu.tms.mock.service.*;
import com.atguigu.tms.mock.service.adv.impl.AdvServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 线路基本表 服务实现类
 * </p>
 *
 * @author zhangchen
 * @since 2022-06-18
 */
@Service
public class LineBaseShiftServiceImpl extends AdvServiceImpl<LineBaseShiftMapper, LineBaseShift> implements LineBaseShiftService {

    @Autowired
    LineBaseInfoService lineBaseInfoService;

    @Autowired
    TruckTeamService truckTeamService;

    @Autowired
    TruckDriverService truckDriverService;

    @Autowired
    TruckInfoService truckInfoService;



    public void initLineBaseShift() {
        Map<Long, LineBaseInfo> lineBaseInfoMap = lineBaseInfoService.all(true);
        Integer[] shiftClocks = AppConfig.shift_clock;

        for (LineBaseInfo lineBaseInfo  : lineBaseInfoMap.values()) {

            if(lineBaseInfo.getLineLevel()==1){
                LineBaseShift lineBaseShiftMorning  = initLineBaseShiftOne(lineBaseInfo, shiftClocks[0].toString());  //早班
                initLineBaseShiftB(  lineBaseInfo,shiftClocks[2].toString(),  lineBaseShiftMorning);//晚班
                LineBaseShift lineBaseShiftNoon  = initLineBaseShiftOne(lineBaseInfo, shiftClocks[1].toString());  //中班
                initLineBaseShiftB(  lineBaseInfo,shiftClocks[3].toString(),  lineBaseShiftNoon);//夜班
            }else{
                String lineNo = lineBaseInfo.getLineNo();
                String lastNo = StringUtils.substring(lineNo, lineNo.length() - 1, lineNo.length());
                if(lastNo.equals("F")){
                    LineBaseShift lineBaseShiftMorning  = initLineBaseShiftOne(lineBaseInfo, shiftClocks[0].toString());  //早班
                    initLineBaseShiftB(  lineBaseInfo,shiftClocks[1].toString(),  lineBaseShiftMorning);//中班
                    LineBaseShift lineBaseShiftNoon  = initLineBaseShiftOne(lineBaseInfo, shiftClocks[1].toString());  //中班
                    initLineBaseShiftB(  lineBaseInfo,shiftClocks[2].toString(),  lineBaseShiftNoon);//晚班
                }

            }

         }


    }

    @Override
    public   LineBaseShift  getNextShiftByLineId(Long lineId, Integer curHour) {
        String curTime = StringUtils.leftPad(curHour + "", 2, "0")+"00:00";
        LineBaseShift nextShift  = baseMapper.getNextShiftByLineId(lineId, curTime);


        return nextShift;
    }

    @Override
    public List<LineBaseShift> getNextShiftList(Integer curHour) {
        String curTime = StringUtils.leftPad(curHour + "", 2, "0")+":00:00";

        List<LineBaseShift> lineBaseShiftList = list(new QueryWrapper<LineBaseShift>().eq("start_time", curTime));


        return lineBaseShiftList;
    }


    public LineBaseShift   initLineBaseShiftOne(LineBaseInfo lineBaseInfo,String shiftClock ){

        String startTime = StringUtils.leftPad(shiftClock + "", 2, "0")+":00:00";
        LineBaseShift lineBaseShift = new LineBaseShift();
        lineBaseShift.setLineId(lineBaseInfo.getId());
        lineBaseShift.setStartTime(startTime);

        lineBaseShift.setLineId(lineBaseInfo.getId());



        TruckInfo truckInfo = truckInfoService.initTruckInfo(lineBaseInfo.getOrgId(),  lineBaseInfo.getLineLevel());
        lineBaseShift.setTruckId(truckInfo.getId());

        TruckDriver truckDriver1 = truckDriverService.initTruckDriver(lineBaseInfo.getOrgId());
        lineBaseShift.setDriver1EmpId(truckDriver1.getEmpId());
        if(lineBaseInfo.getDistance().intValue()>=AppConfig.line_new_2_driver_km){
            TruckDriver truckDriver2 = truckDriverService.initTruckDriver(lineBaseInfo.getOrgId());
            lineBaseShift.setDriver2EmpId(truckDriver2.getEmpId());
        }

        saveOrUpdate(lineBaseShift,true);
        return  lineBaseShift;
    }


    public LineBaseShift   initLineBaseShiftB(LineBaseInfo lineBaseInfoA,String shiftClock ,LineBaseShift lineBaseShiftA) {
        Long pairLineId = lineBaseInfoA.getPairLineId();
        LineBaseInfo lineBaseInfoB = lineBaseInfoService.getById(pairLineId, true);
        String startTime = shiftClock +":00:00";

        LineBaseShift lineBaseShiftB = new LineBaseShift();
        lineBaseShiftB.setLineId(lineBaseInfoB.getId());
        lineBaseShiftB.setStartTime(startTime);

        lineBaseShiftB.setTruckId(lineBaseShiftA.getTruckId());
        lineBaseShiftB.setDriver1EmpId(lineBaseShiftA.getDriver1EmpId());
        lineBaseShiftB.setDriver2EmpId(lineBaseShiftA.getDriver2EmpId());

        lineBaseShiftB.setPairShiftId(lineBaseShiftA.getId());

        saveOrUpdate(lineBaseShiftB,true);
        lineBaseShiftA.setPairShiftId(lineBaseShiftB.getId());
        saveOrUpdate(lineBaseShiftA,true);

        return  lineBaseShiftB;
    }

    public  void getEstArriveTime(){


    }

}
